<!--轮播基础组件-->

<template>
  <div class="slider" ref="slider">
    <div class="slider-group" ref="sliderGroup">
      <slot>
        <!--外部引入slider slider包裹的DOM就会被插槽到此处-->
      </slot>
    </div>
    <div class="dots">
      <span class="dot" v-for="(item,index) in dots" :class="{active: currentPagIndex === index}"></span>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  import {addClass} from "common/js/dom";

  export default {
        name: "slider",
    data() {
          return {
            dots: [], // 轮播小圆点
            currentPagIndex: 0 // 小圆点默认当前页数
          }
    },
      props: {
          // 循环
        loop: {
          type: Boolean,
          default: true
        },
        // 自动播放
        autoPlay: {
            type: Boolean,
          default: true
        },
        // 时间间隔
        interval: {
            type: Number,
          default: 4000
        }
      },
      mounted() {
          // BS20毫秒初始化
        setTimeout(() => {
          this._setSliderWidth();
          this._initDots();
          this._initSlider();

          if (this.autoPlay) {
            this._play()
          }
        },20)

        // 优化slider 当切换不同屏幕时 slider发生问题
        window.addEventListener('resize', () => {
          // 当slider未渲染时
          if (!this.slider) {
            return
          }
          this._setSliderWidth(true) // 切换后会增加2倍宽度
          this.slider.refresh()
        })
      },
      methods: {
        // 初始化slider宽度
        _setSliderWidth(isResize) {
          this.children = this.$refs.sliderGroup.children;  // 获取sliderGroup的子元素
          let width = 0
          let sliderWidth = this.$refs.slider.clientWidth
          for (let i=0; i< this.children.length; i++) {
             let child = this.children[i]
            // 此处需要自己添加slider-item样式
             addClass(child, 'slider-item')
            // 设置child的宽度 === child宽度等于父容器宽度
            child.style.width = sliderWidth + 'px'
            width += sliderWidth
          }
          // 假如为loop 加两倍width loop时会克隆两张图片
          // 开始不传 !undifined === true !true === false
          if (this.loop && !isResize) {
            width += 2*sliderWidth
          }
          this.$refs.sliderGroup.style.width = width + 'px'

        },
        _initSlider() {
          // 使用BS
          this.slider = new BScroll(this.$refs.slider,{
            scrollX: true,
            scrollY: false,
            momentun: false,
            snap: true,
            snapLoop: this.loop,
            snapThreshold: 0.3,
            snapSpeed: 400,
            click: true
          })
          // 使用BS派发scrollEnd事件获取当前index
          this.slider.on('scrollEnd',() => {
            let pageIndex = this.slider.getCurrentPage().pageX
            if (this.loop) {
              pageIndex -= 1
            }
            this.currentPagIndex = pageIndex
            if (this.autoPlay) {
              clearTimeout(this.timer)
              this._play()
            }
          })
        },
        _initDots() {
          this.dots = new Array(this.children.length)
        },
        _play() {
          let pageIndex = this.currentPagIndex + 1 // 索引从0开始
          if (this.loop) {
            pageIndex += 1
          }
          this.timer = setTimeout(() => {
            this.slider.goToPage(pageIndex, 0, 400) // x y 间隔
          },this.interval)
        }
      },
      destroyed() {
            clearTimeout(this.timer)
      }
    }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .slider
    min-height: 1px
    .slider-group
      position: relative
      overflow: hidden
      white-space: nowrap
      .slider-item
        float: left
        box-sizing: border-box
        overflow: hidden
        text-align: center
        a
          display: block
          width: 100%
          overflow: hidden
          text-decoration: none
        img
          display: block
          width: 100%
    .dots
      position: absolute
      right: 0
      left: 0
      bottom: 12px
      text-align: center
      font-size: 0
      .dot
        display: inline-block
        margin: 0 4px
        width: 8px
        height: 8px
        border-radius: 50%
        background: $color-text-l
        &.active
          width: 20px
          border-radius: 5px
          background: $color-text-ll
</style>
